<?php

include_once APPLICATION_DIR.'/application/model/business/validator/base/PointSaleValidation.class.php';
include_once APPLICATION_DIR.'/application/model/utils/ValidateUtil.class.php';

class CreatePointSaleValidation extends PointSaleValidation{
    
    public function exec() {
        //Prepare data
        include_once APPLICATION_DIR.'/application/data/country_phone.php';
        global $_COUNTRY_PHONE;
        
        $point_sale = $this->rto;
        $cityRM = new CityRM();
        $countryRM = new CountryRM();
        $supplierRM = new SupplierRM();
        if($point_sale->PHONE != ''){
            $phones = explode(".", $point_sale->PHONE );
        }
        
        //Validate
        if($point_sale->NAME ==  ''){
            $this->addErrorMessage("_MSG_POINT_SALE_NOT_EMPTY");
        }else if(strlen ($point_sale->NAME) > MAX_LENGTH_POINT_SALE_NAME ){
            $this->addErrorMessage("_MSG_POINT_SALE_NAME_OVER_MAX_LENGTH_ALLOW ");
        }
        
        if($point_sale->STREET ==  ''){
            $this->addErrorMessage("_MSG_STREET_NOT_EMPTY");
        }else if(strlen ($point_sale->STREET) > MAX_LENGTH_LOCATION_ADDRESS ){
            $this->addErrorMessage("_MSG_STREET_OVER_MAX_LENGTH_ALLOW");
        }
        
        if(strlen ($point_sale->PHONE) > MAX_LENGTH_PHONE_NUMBER){
            $this->addErrorMessage("_MSG_PHONE_NUMBER_OVER_MAX_LENGTH_ALLOW");
        }else if( ($point_sale->PHONE != '' && count($phones) != 2)
                || (isset($phones[0]) && !isset($_COUNTRY_PHONE[$phones[0]]))  
                || (isset($phones[1])  && !ValidateUtil::isValidTelephone($phones[1])) ){
            $this->addErrorMessage("_MSG_INVALID_PHONE");
        }
        
        if(strlen ($point_sale->EMAIL) > MAX_LENGTH_EMAIL){
            $this->addErrorMessage("_MSG_EMAIL_OVER_MAX_LENGTH_ALLOW");
        }else if($point_sale->EMAIL != '' && !ValidateUtil::isEmail($point_sale->EMAIL)){
            $this->addErrorMessage("_MSG_INVALID_EMAIL");
        }
        
        if($point_sale->CITY_ID ==  0 
                || !CityRM::isExisted($cityRM->getName(),array("ID"=>$point_sale->CITY_ID,"ENABLED"=>  CityRM::ENABLED)) ){
            $this->addErrorMessage("_MSG_CITY_NOT_EXIST");
        }
        
        if($point_sale->COUNTRY_ID ==  0 
                || !CountryRM::isExisted($countryRM->getName(),array("ID"=>$point_sale->COUNTRY_ID,"ENABLED"=>CountryRM::ENABLED))){
            $this->addErrorMessage("_MSG_COUNTRY_NOT_EXIST");
        }
        
        if($point_sale->SUPPLIER_ID ==  0 
                || !SupplierRM::isExisted($supplierRM->getName(),array("ID"=>$point_sale->SUPPLIER_ID,"ENABLED"=>SupplierRM::ENABLED))){
            $this->addErrorMessage("_MSG_SUPPLIER_NOT_EXIST");
        }
        
        if($point_sale->X ==  null || $point_sale->Y ==  null){
            $this->addErrorMessage("_MSG_INVALID_LOCATION");
        }
        
        if($this->getErrorMessage()->hasError()){
            return false;
        }
        return true;
    }
}

?>
